package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.UpdateAddressActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.UserAddressListActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserAddressBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.AddUserAddressModel;

public class UserAddressListAdapter extends BaseAdapter<UserAddressBean> {
    String userid;

    public UserAddressListAdapter(Context context, List<UserAddressBean> items, int layoutResourse,String userid) {
        super(context, items, layoutResourse);
        this.userid = userid;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        UserAddressListViewHolder viewHolder = new UserAddressListViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        UserAddressListViewHolder viewHolder=(UserAddressListViewHolder) holder;
        UserAddressBean userAddressBean=items.get(position);

        final String username = userAddressBean.getUseraddress_username();
        final String phone = userAddressBean.getUseraddress_phone();
        final String address = userAddressBean.getUseraddress_city()+userAddressBean.getUseraddress_address();
        final String id = userAddressBean.getUseraddress_id();

        viewHolder.addressuser_username.setText(username);
        viewHolder.addressuser_phone.setText(phone);
        viewHolder.addressuser_address.setText(address);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog dlg = new AlertDialog.Builder(context)//builder创建对话框
                        .setIcon(R.mipmap.kpl)//显示头像
                        .setTitle("温馨提示")//显示标题
                        .setMessage("选择操作")//显示类容
                        .setNegativeButton("取消",null)
                        .setNeutralButton("修改", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent intent = new Intent(context, UpdateAddressActivity.class);
                                intent.putExtra("useraddressid",id);
                                context.startActivity(intent);
                            }
                        })
                        .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                AddUserAddressModel addUserAddressModel = new AddUserAddressModel();
                                addUserAddressModel.deleteUserAddressById(userid,id,successBeanTListener);
                            }
                        })//按钮，null可以选择点击后要执行的事情
                        .create();//创建对话框
                dlg.show();
            }
        });


    }
    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            if (successBean.getSuccess().equals("1")){
                Toast.makeText(context,"删除成功", Toast.LENGTH_SHORT).show();
                if(UserAddressListActivity.class.isInstance(context)){

                    UserAddressListActivity activity = (UserAddressListActivity)context;
                    activity.finish();}
            }
            else {
                Toast.makeText(context,"删除失败", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFail(String msg) {

        }
    };
}
